<?php
/**
 * Created by PhpStorm.
 * User: longli
 * Date: 2022/02/18
 * Time: 10:24
 * @link http://www.lmterp.cn
 */

namespace app\admin\controller\wms;

use app\admin\controller\BaseController;
use app\common\service\import\ProductDevelop;
use app\common\service\product\ProductService;
use think\facade\Log;

/**
 * 产品开发
 * Class DevelopController
 * @package app\admin\controller\wms
 */
class DevelopController extends BaseController
{
    /**
     * 首页
     * @date 2022/02/18
     * @author longli
     */
    public function index()
    {
        if($this->request->isAjax())
        {
            $develop = \app\common\model\ProductDevelop::order('id desc');
            $this->searchModel($develop, [
                'times' => ['create_time']
            ]);
            $limit = $this->getPageSize();
            $develop = $develop->paginate($limit);
            $this->assign("list", $develop->getCollection());
            $this->assign("page", $develop->render());
            $this->assign("has_claim", $this->user->hasPermissions('/wms/develop/claim'));
            $this->assign("has_delete", $this->user->hasPermissions('/wms/develop/delete'));
            return $this->fetch('lists');
        }
        return $this->fetch('index');
    }

    /**
     * 详情页
     * @date 2022/02/18
     * @author longli
     */
    public function info()
    {
        $id = $this->request->get("id");
        $develop = \app\common\model\ProductDevelop::get($id);
        $this->assign("develop", $develop);
        return $this->fetch('info');
    }

    /**
     * 认领到产品库
     * @date 2022/02/18
     * @author longli
     */
    public function claim()
    {
        $id = $this->request->post("id");
        $account = $this->request->post("account", '', 'trim');
        if(empty($account)) $this->error("销售账号必填");
        $develop = \app\common\model\ProductDevelop::get($id);
        if(empty($develop)) $this->error("非法操作");
        $develop->checked_belone = $account;
        $develop->save();
        $this->success("认领成功");
    }

    /**
     * 导入
     * @date 2022/02/18
     * @author longli
     */
    public function import()
    {
        $path = ProductService::getRootPath($this->request->request("path"));
        try{
            $develop = new ProductDevelop($path);
            $develop->readFile();
            $develop->run();
        }catch(\Exception $e)
        {
            Log::error("开发产品发入表格失败，提示信息【" . $e->getMessage() . "】");
        }
        $this->success("导入成功");
    }

    /**
     * 导出
     * @date 2022/02/18
     * @author longli
     */
    public function export()
    {
        $ids = $this->request->get("ids");
        $where = [];
        if(!empty($ids)) $where[] = ["id", "in", $ids];
        $export = new \app\common\service\export\ProductDevelop();
        $file = $export->runExcel($where);
        $this->success("导出成功", '', ["src" => urlencode($file)]);
    }

    /**
     * 试算成本
     * @date 2022/02/22
     * @author longli
     */
    public function calculate()
    {
        $ids = $this->request->get("ids");
        $develops = \app\common\model\ProductDevelop::whereIn("id", $ids)->select();
        $total = 0;
        foreach($develops as $develop)
        {

        }
        $this->assign("develops", $develops);
        $this->assign("total", $total);
        return $this->fetch("calculate");
    }

    /**
     * 删除
     * @date 2022/02/24
     * @author longli
     */
    public function delete()
    {
        $ids = $this->request->request("ids");
        if(empty($ids)) $this->error("非法操作");
        \app\common\model\ProductDevelop::destroy($ids);
        $this->success("删除成功");
    }
}